package cn.hpclub.server.controller.admin;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;

import cn.hpclub.server.constant.Constant;
import cn.hpclub.server.model.Business;
import cn.hpclub.server.validator.admin.BusinessValidator;

public class BusinessController extends BaseAdminController<Business>{

    static int UI_PAGER_NUMBER_DEFAULT = 1;// UI默认起始页
    static int UI_PAGER_SIZE_DEFAULT = 10;// UI页容量

    static String UI_EDIT = "edit"; // UI编辑标识

    public void list(){
        String search = "";
        Object[] p;
        search += " and " + Business.BUSINESS_CODE + " like ?";
        search += " and " + Business.BUSINESS_NAME + " like ?";
        if(!this.getPara(Business.ENABLED, "-1").equals("-1")){
            search += " and " + Business.ENABLED + "=?";
            p = new Object[] { "%" + this.getPara(Business.BUSINESS_CODE, "") + "%",
                    "%" + this.getPara(Business.BUSINESS_NAME, "") + "%", this.getPara(Business.ENABLED, "-1") };
        } else{
            p = new Object[] { "%" + this.getPara(Business.BUSINESS_CODE, "") + "%",
                    "%" + this.getPara(Business.BUSINESS_NAME, "") + "%" };
        }
        Page<Record> pager = Db
                .paginate(getParaToInt(Constant.Ui.PAGER_NUMBER, UI_PAGER_NUMBER_DEFAULT),
                          getParaToInt(Constant.Ui.PAGER_SIZE, UI_PAGER_SIZE_DEFAULT), "select *",
                          "from " + Constant.Table.BUSIENSS + " where " + Business.IS_DELETED + "=0" + search, p);

        setAttr(Constant.Ui.PAGER, pager);
        setAttr(Business.ENABLED, this.getPara(Business.ENABLED, "-1"));
        setAttr(Business.BUSINESS_CODE, this.getPara(Business.BUSINESS_CODE, ""));
        setAttr(Business.BUSINESS_NAME, this.getPara(Business.BUSINESS_NAME, ""));

        render("/admin/business_list.tpl");
    }

    public void reflash(int num, int size, String select, String from){
        Page<Record> pager = Db.paginate(num, size, select, from);
        setAttr(Constant.Ui.PAGER, pager);
    }

    public void add(){

        Record pager = null;

        pager = new Record();
        setAttr(Business.ID, 0);
        setAttr(Business.BUSINESS_CODE, "");
        setAttr(Business.BUSINESS_NAME, "");
        setAttr(Business.PORT_URL, "");
        setAttr(Business.ENABLED, 1);
        setAttr(Business.REMARK, "");
        setAttr(Business.BUSINESS_TYPE, 0);
        setAttr(Business.AMOUNT, "0");

        setAttr(UI_EDIT, 0);
        // setAttr(UI_PAGER, pager);
        render("/admin/business_add.tpl");
    }

    public void edit(){

        Record pager = null;

            pager = Db.paginate(1, 1, "select *", "from " + Constant.Table.BUSIENSS + " where " + Business.ID + "=?",
                                new Object[] { getParaToInt(Business.ID, 0) })
                .getList().get(0);
            setAttr(Business.ID, pager.getInt(Business.ID));
            setAttr(Business.BUSINESS_CODE, pager.getStr(Business.BUSINESS_CODE));
            setAttr(Business.BUSINESS_NAME, pager.getStr(Business.BUSINESS_NAME));
            setAttr(Business.PORT_URL, pager.getStr(Business.PORT_URL));
            setAttr(Business.ENABLED, pager.getInt(Business.ENABLED));
            setAttr(Business.REMARK, pager.getStr(Business.REMARK));
            setAttr(Business.BUSINESS_TYPE, pager.getInt(Business.BUSINESS_TYPE));
            setAttr(Business.AMOUNT, pager.getBigDecimal(Business.AMOUNT));
        setAttr(UI_EDIT, 1);
        // setAttr(UI_PAGER, pager);
        render("/admin/business_add.tpl");
    }

    @Before({ BusinessValidator.class, Tx.class })
    public void update(){
        
        if(getPara(UI_EDIT, "0").equals("1")){
            Db.update("update " + Constant.Table.BUSIENSS + " set " + Business.BUSINESS_CODE + "=?,"
                    + Business.BUSINESS_NAME + "=?," + Business.PORT_URL + "=?," + Business.ENABLED + "=?,"
                    + Business.REMARK + "=?" + " where " + Business.ID + "=?",
                      new Object[] { getPara(Business.BUSINESS_CODE, null), getPara(Business.BUSINESS_NAME, null),
                              getPara(Business.PORT_URL, null), getPara(Business.ENABLED, null),
                              getPara(Business.REMARK, null), getPara(Business.ID, null) });
        } else{
            Db.update("insert into " + Constant.Table.BUSIENSS + "(" + Business.BUSINESS_CODE + ","
                    + Business.BUSINESS_NAME + "," + Business.PORT_URL + "," + Business.ENABLED + "," + Business.REMARK
                    + ")" + " value(?,?,?,?,?)",
                      new Object[] { getPara(Business.BUSINESS_CODE, null), getPara(Business.BUSINESS_NAME, null),
                              getPara(Business.PORT_URL, null), getPara(Business.ENABLED, null),
                              getPara(Business.REMARK, null) });
        }
        setAttr(Business.ENABLED, "-1");
        setAttr(Business.BUSINESS_CODE, "");
        setAttr(Business.BUSINESS_NAME, "");

        this.redirect("/business/list");
    }

    @Before({ Tx.class })
    public void delete(){
        try{

            String[] ids = getParaValues(Constant.IDS);
            String in = "(";
            for(int i = 0; i < ids.length; i++){
                in += "?,";
            }
            if(in.length() >= 1){
                in = in.substring(0, in.length() - 1) + ")";
        }

            Db.update("update " + Constant.Table.BUSIENSS + " set " + Business.IS_DELETED + "=1 where " + Business.ID
                    + " in " + in, ids);
            ajaxJsonSuccessMessage(Constant.Msg.DEL_DONE);
        } catch(Exception e){
            ajaxJsonErrorMessage(Constant.Msg.DEL_FAILED);
        }
    }

}
